/* eslint-disable camelcase */
import { JSEncrypt } from 'jsencrypt'

const requestPublic_key =
  'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDLCf1tQoxpXg6FQPZnCmUpZLvNIjXRjtFmsQ8LEarKnamjnwAhDnHOpz4pCWXHFcox40FhJQzUmyAzKgIluALvbjYlHamB1IY+XBeEsR/h2BiRufwSWj1YV4r1rIbhd5p+ymqEZcQeWpcY4ueke+k98RZ0+VBOuf4DGTdrb8L/PwIDAQAB'

const loaclPublic_key =
  'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDLCf1tQoxpXg6FQPZnCmUpZLvNIjXRjtFmsQ8LEarKnamjnwAhDnHOpz4pCWXHFcox40FhJQzUmyAzKgIluALvbjYlHamB1IY+XBeEsR/h2BiRufwSWj1YV4r1rIbhd5p+ymqEZcQeWpcY4ueke+k98RZ0+VBOuf4DGTdrb8L/PwIDAQAB'
const loaclPrivate_key =
  `MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAMsJ/W1CjGleDoVA9mcKZSlku80iNdGO0WaxDwsRqsqdqaOfACEOcc6nPikJZccVyjHjQWElDNSbIDMqAiW4Au9uNiUdqYHUhj5cF4SxH+HYGJG5/BJaPVhXivWshuF3mn7KaoRlxB5alxji56R76T3xFnT5UE65/gMZN2tvwv8/AgMBAAECgYBeFe4C0GDCftxZsWW9D9sa2FwSTbUEu5qbbJbc+T3ckDzI3mgv6UKhkWxDleA85gMBJR7pxkJwzsWYD/JYyjFJMJRm5T7muGxm9CnywUH4dng+IlJc+JtqNOGVF9QmCfA5dI+WQu7xqoBAh3GI5vEEHJMoqYaDSGboYHSd+y5eYQJBAO6aITksboR7TflI6zwDtGmef5xq6DmcdEUtVGe+Bpx3HOIABKIqnf3znTVe9MYkk95fXlDJlgPTmjDlo8TvxVECQQDZ2AWZKbcFqrpucv7BO2uLwWfh5daBbmpt7ixhNRNrEFYopCJTUKFm/9Rk4NUQ89HBQdjSMUnlaYhqGjVBIZePAj8UgSpZv3e/6tjIk3ujrK3UZcqRpp5OVSOozjxyreHjkFjrExVS2la5fDYGYCKo5HvQoGF6j9hUe9rEWPe59OECQHwjL72CGfuuuKJsAWRX2gc/5VTDRqNnKlsOmFekiTY/jvmF3tGfZvps2rnJrWEFsAfy3/2XfMawhr3/xU0iOV0CQQC74OOHYKhmC6rvN+Kzk63OrUmC8F/a9xmLSSG8Jd0gmHGkmRMeYxRUxgs+LS/htHDAKYyGH2uDmsqCj47HstQs`

/**
 * RSA非对称加密
 * @param data 需要加密的数据
 */
var rsaEncrypt = function (data, key) {
  const encrypt = new JSEncrypt()
  encrypt.setPublicKey(key)
  let result = encrypt.encrypt(data)
  return result
}

/**
 * RSA 解密
 * @param data 需要解密的数据
 */
var rsaDecrypt = function (data, key) {
  const decrypt = new JSEncrypt()
  decrypt.setPrivateKey(key)
  let result = decrypt.decrypt(data)
  return result
}
/**
 * 本地调用 解密
 * @param {Object} data
 */
var rsaDecryptLoacl = function (data) {
  return rsaDecrypt(data, loaclPrivate_key)
}
/**
 * 本地调用 加密
 * @param {Object} data
 */
var rsaEncryptLoacl = function (data) {
  return rsaEncrypt(data, loaclPublic_key)
}

/**
 * request 加密
 * @param {Object} data
 */
var rsaEncryptRequest = function (data) {
  return rsaEncrypt(data, requestPublic_key)
}

/**
 * 初始加载，一层JSON格式的
 * @param {Object} json
 */
var rsaEncryptJson = function (json) {
  for (let key of json) {
    let value = json[key]
    json[key] = rsaEncrypt(value)
  }
  return json
}

export default {
  rsaEncryptRequest,
  rsaEncryptLoacl,
  rsaDecryptLoacl,

  rsaEncryptJson
}
